#ifndef SROS_DEVICE_MCU_CONFIG_HPP
#define SROS_DEVICE_MCU_CONFIG_HPP

#include <string>
#include <commons/config/config.hpp>

namespace sros::device::cfg
{
    struct CanItem
    {
        uint16_t index = 1;
        uint16_t prescaler = 6;
        uint8_t seg1 = 7;
        uint8_t seg2 = 6;
        NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(CanItem, index, prescaler, seg1, seg2)
    };

    struct UartItem
    {
        uint16_t index = 1;
        uint32_t bitrate = 115200;
        uint8_t wordlength = 8;
        uint8_t stopbits = 1;
        uint8_t parity = 'N';
        NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(UartItem, index, bitrate, wordlength, stopbits, parity)
    };

    struct InputIoItem
    {
        uint8_t port = 'E';
        uint32_t pin = 10;
        uint32_t pull_up_down = 0;
        std::string name = "";
        NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(InputIoItem, port, pin, pull_up_down, name)
    };

    struct OutputIoItem
    {
        uint8_t port = 'E';
        uint32_t pin = 10;
        uint32_t mode = 0;
        uint32_t pull_up_down = 0;
        uint32_t out_level = 0;
        uint32_t spd = 0;
        std::string name;
        NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(OutputIoItem, port, pin, mode, pull_up_down, out_level, spd, name)
    };

    // MCU配置
    struct McuCfg
    {
        std::string remote_ip;                 // 远端ip地址
        uint16_t remote_port;                  // 远端端口
        uint32_t max_retry_conn_times = 0;     // 0: 一直重连;   > 0: 重接次数
        uint32_t cycle_retry_conn_mill = 3000; // 尝试重新连接的时间间隔 (ms)
        bool retry_conn_enable = true;         // 开启重连接功能
        uint32_t conn_timeout_mill = 1200;     // 连接超时时间 (ms)

        std::vector<CanItem> cans;           // CAN口配置
        std::vector<UartItem> uarts;         // 串口配置
        std::vector<InputIoItem> inputios;   // 输入IO配置
        std::vector<OutputIoItem> outputios; // 输出IO配置

        NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(McuCfg,
                                                    remote_ip, remote_port,
                                                    max_retry_conn_times,
                                                    cycle_retry_conn_mill,
                                                    retry_conn_enable,
                                                    conn_timeout_mill,
                                                    cans, uarts, inputios, outputios)
    };
}

#endif